import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;


public class BettingFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textFieldSingle;
	private JTextField textFieldBigSingle;
	private JTextField textFieldBigDouble;
	private JTextField textFieldSmallSingle;
	private JTextField textFieldSmallDouble;
	private JTextField textFieldEachPoint;
	private JComboBox<String> cmbBoxName; 
	private JLabel labelRestPoints;
	private JComboBox<Integer> cmbBoxEachPoint;
	private JTextField textFieldDouble;
	private JTextField textFieldSmall;
	private JTextField textFieldBig;

	private User user = null;
	private int singleValue = 0;
	private int doubleValue = 0;
	private int smallValue = 0;
	private int bigValue = 0;
	private int bigSingleValue = 0;
	private int bigDoubleValue = 0;
	private int smallSingleValue = 0;
	private int smallDoubleValue = 0;
	private int eachSinglePoint = 0;
	private int eachSinglePointValue = 0;
	private int[] eachPintArray = new int[User.NUM];
	private boolean isConfirmed = true;

	/**
	 * Create the frame.
	 */
	public BettingFrame() {
		for(int i = 0; i < User.NUM; i++){
			eachPintArray[i] = 0;
		}
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 400);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		cmbBoxName = new JComboBox<String>();
		//TODO the event listener  is right?
		cmbBoxName.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				confirmBetting();
				paintView();
			}
		});
		cmbBoxName.setBounds(93, 41, 184, 24);
		cmbBoxName.setEditable(false);
		contentPane.add(cmbBoxName);
		
		JLabel label = new JLabel("用户");
		label.setFont(new Font("Dialog", Font.BOLD, 20));
		label.setBounds(41, 39, 52, 24);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("剩余");
		label_1.setFont(new Font("Dialog", Font.BOLD, 20));
		label_1.setBounds(295, 39, 42, 24);
		contentPane.add(label_1);
		
		labelRestPoints = new JLabel("0");
		labelRestPoints.setFont(new Font("Dialog", Font.BOLD, 20));
		labelRestPoints.setBounds(349, 39, 165, 24);
		contentPane.add(labelRestPoints);
		
		JButton buttonBetting = new JButton("确认下注");
		buttonBetting.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				confirmBetting();
				JOptionPane.showMessageDialog(null, "用户:" + user.getName() + "下注成功");
			}
		});
		buttonBetting.setFont(new Font("Dialog", Font.BOLD, 20));
		buttonBetting.setBounds(41, 316, 128, 24);
		contentPane.add(buttonBetting);
		
		JLabel label_4 = new JLabel("单");
		label_4.setFont(new Font("Dialog", Font.BOLD, 20));
		label_4.setBounds(41, 100, 21, 24);
		contentPane.add(label_4);
		
		textFieldSingle = new JTextField();
		textFieldSingle.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				stopCharacter(e);
			}
			@Override
			public void keyReleased(KeyEvent e) {
				user.addRestPoints(singleValue);
				String value = textFieldSingle.getText();
				if(value.length() > 0){
					singleValue = Integer.valueOf(value);
					user.subRestPoints(singleValue);
				}else{
					singleValue = 0;
				}
				labelRestPoints.setText(user.getRestPoints() + "");
			}
		});
		textFieldSingle.setFont(new Font("Dialog", Font.PLAIN, 20));
		textFieldSingle.setBounds(80, 97, 146, 31);
		contentPane.add(textFieldSingle);
		textFieldSingle.setColumns(10);
		
		JLabel label_7 = new JLabel("大单");
		label_7.setFont(new Font("Dialog", Font.BOLD, 20));
		label_7.setBounds(29, 172, 42, 31);
		contentPane.add(label_7);
		
		textFieldBigSingle = new JTextField();
		textFieldBigSingle.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				stopCharacter(e);
				
			}
			@Override
			public void keyReleased(KeyEvent e) {
				user.addRestPoints(bigSingleValue);
				String value = textFieldBigSingle.getText();
				if(value.length() > 0){
					bigSingleValue = Integer.valueOf(value);
					user.subRestPoints(bigSingleValue);
				}else{
					bigSingleValue = 0;
				}
				labelRestPoints.setText(user.getRestPoints() + "");
			}
		});
		textFieldBigSingle.setFont(new Font("Dialog", Font.PLAIN, 20));
		textFieldBigSingle.setColumns(10);
		textFieldBigSingle.setBounds(80, 172, 146, 31);
		contentPane.add(textFieldBigSingle);
		
		JLabel label_8 = new JLabel("大双");
		label_8.setFont(new Font("Dialog", Font.BOLD, 20));
		label_8.setBounds(234, 170, 52, 35);
		contentPane.add(label_8);
		
		textFieldBigDouble = new JTextField();
		textFieldBigDouble.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				stopCharacter(e);
				
			}
			@Override
			public void keyReleased(KeyEvent e) {
				user.addRestPoints(bigDoubleValue);
				String value = textFieldBigDouble.getText();
				if(value.length() > 0){
					bigDoubleValue = Integer.valueOf(value);
					user.subRestPoints(bigDoubleValue);
				}else{
					bigDoubleValue = 0;
				}
				labelRestPoints.setText(user.getRestPoints() + "");
			}
		});
		textFieldBigDouble.setFont(new Font("Dialog", Font.PLAIN, 20));
		textFieldBigDouble.setColumns(10);
		textFieldBigDouble.setBounds(282, 172, 146, 31);
		contentPane.add(textFieldBigDouble);
		
		JLabel label_9 = new JLabel("小单");
		label_9.setFont(new Font("Dialog", Font.BOLD, 20));
		label_9.setBounds(29, 214, 42, 31);
		contentPane.add(label_9);
		
		textFieldSmallSingle = new JTextField();
		textFieldSmallSingle.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				stopCharacter(e);
			}
			@Override
			public void keyReleased(KeyEvent e) {
				user.addRestPoints(smallSingleValue);
				String value = textFieldSmallSingle.getText();
				if(value.length() > 0){
					smallSingleValue = Integer.valueOf(value);
					user.subRestPoints(smallSingleValue);
				}else{
					smallSingleValue = 0;
				}
				labelRestPoints.setText(user.getRestPoints() + "");
			}
		});
		textFieldSmallSingle.setFont(new Font("Dialog", Font.PLAIN, 20));
		textFieldSmallSingle.setColumns(10);
		textFieldSmallSingle.setBounds(80, 215, 146, 31);
		contentPane.add(textFieldSmallSingle);
		
		JLabel label_10 = new JLabel("小双");
		label_10.setFont(new Font("Dialog", Font.BOLD, 20));
		label_10.setBounds(234, 212, 42, 35);
		contentPane.add(label_10);
		
		textFieldSmallDouble = new JTextField();
		textFieldSmallDouble.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				stopCharacter(e);
			}
			@Override
			public void keyReleased(KeyEvent e) {
				user.addRestPoints(smallDoubleValue);
				String value = textFieldSmallDouble.getText();
				if(value.length() > 0){
					smallDoubleValue = Integer.valueOf(value);
					user.subRestPoints(smallDoubleValue);
				}else{
					smallDoubleValue = 0;
				}
				labelRestPoints.setText(user.getRestPoints() + "");
			}
		});
		textFieldSmallDouble.setFont(new Font("Dialog", Font.PLAIN, 20));
		textFieldSmallDouble.setColumns(10);
		textFieldSmallDouble.setBounds(280, 217, 148, 31);
		contentPane.add(textFieldSmallDouble);
		
		JLabel label_11 = new JLabel("单点");
		label_11.setFont(new Font("Dialog", Font.BOLD, 20));
		label_11.setBounds(29, 258, 52, 24);
		contentPane.add(label_11);
		
		cmbBoxEachPoint = new JComboBox<Integer>();
		cmbBoxEachPoint.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				eachPintArray[eachSinglePoint] = eachSinglePointValue;
				eachSinglePoint = (Integer)cmbBoxEachPoint.getSelectedItem();
				eachSinglePointValue = eachPintArray[eachSinglePoint];
				if(eachSinglePointValue == 0){
					textFieldEachPoint.setText(null);
				}else{
					textFieldEachPoint.setText(eachSinglePointValue + "");
				}
			}
		});
		cmbBoxEachPoint.setModel(new DefaultComboBoxModel<Integer>(new Integer[] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27}));
		cmbBoxEachPoint.setFont(new Font("Dialog", Font.BOLD, 20));
		cmbBoxEachPoint.setBounds(80, 258, 95, 24);
		contentPane.add(cmbBoxEachPoint);
		
		textFieldEachPoint = new JTextField();
		textFieldEachPoint.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				stopCharacter(e);
				
			}
			@Override
			public void keyReleased(KeyEvent e) {
				user.addRestPoints(eachSinglePointValue);
				String value = textFieldEachPoint.getText();
				if(value.length() > 0){
					eachSinglePointValue = Integer.valueOf(value);
					user.subRestPoints(eachSinglePointValue);
				}else{
					eachSinglePointValue = 0;
				}
				labelRestPoints.setText(user.getRestPoints() + "");
			}
		});
		textFieldEachPoint.setFont(new Font("Dialog", Font.PLAIN, 20));
		textFieldEachPoint.setColumns(10);
		textFieldEachPoint.setBounds(187, 258, 128, 31);
		contentPane.add(textFieldEachPoint);
		
		JButton buttonReturn = new JButton("返回主页");
		buttonReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				confirmBetting();
				dispose();
				Launcher.showMainFrame();
			}
		});
		buttonReturn.setFont(new Font("Dialog", Font.BOLD, 20));
		buttonReturn.setBounds(234, 316, 126, 25);
		contentPane.add(buttonReturn);
		
		JLabel label_2 = new JLabel("双");
		label_2.setFont(new Font("Dialog", Font.BOLD, 20));
		label_2.setBounds(245, 100, 21, 24);
		contentPane.add(label_2);
		
		textFieldDouble = new JTextField();
		textFieldDouble.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				stopCharacter(e);
			}
			@Override
			public void keyReleased(KeyEvent e) {
				user.addRestPoints(doubleValue);
				String value = textFieldDouble.getText();
				if(value.length() > 0){
					doubleValue = Integer.valueOf(value);
					user.subRestPoints(doubleValue);
				}else{
					doubleValue = 0;
				}
				labelRestPoints.setText(user.getRestPoints() + "");
			}
		});
		textFieldDouble.setFont(new Font("Dialog", Font.PLAIN, 20));
		textFieldDouble.setColumns(10);
		textFieldDouble.setBounds(282, 97, 146, 31);
		contentPane.add(textFieldDouble);
		
		JLabel label_3 = new JLabel("小");
		label_3.setFont(new Font("Dialog", Font.BOLD, 20));
		label_3.setBounds(41, 136, 21, 24);
		contentPane.add(label_3);
		
		textFieldSmall = new JTextField();
		textFieldSmall.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				stopCharacter(e);
			}
			@Override
			public void keyReleased(KeyEvent e) {
				user.addRestPoints(smallValue);
				String value = textFieldSmall.getText();
				if(value.length() > 0){
					smallValue = Integer.valueOf(value);
					user.subRestPoints(smallValue);
				}else{
					smallValue = 0;
				}
				labelRestPoints.setText(user.getRestPoints() + "");
			}
		});
		textFieldSmall.setFont(new Font("Dialog", Font.PLAIN, 20));
		textFieldSmall.setColumns(10);
		textFieldSmall.setBounds(80, 133, 146, 31);
		contentPane.add(textFieldSmall);
		
		JLabel label_5 = new JLabel("大");
		label_5.setFont(new Font("Dialog", Font.BOLD, 20));
		label_5.setBounds(245, 136, 21, 24);
		contentPane.add(label_5);
		
		textFieldBig = new JTextField();
		textFieldBig.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				stopCharacter(e);
			}
			@Override
			public void keyReleased(KeyEvent e) {
				user.addRestPoints(bigValue);
				String value = textFieldBig.getText();
				if(value.length() > 0){
					bigValue = Integer.valueOf(value);
					user.subRestPoints(bigValue);
				}else{
					bigValue = 0;
				}
				labelRestPoints.setText(user.getRestPoints() + "");
			}
		});
		textFieldBig.setFont(new Font("Dialog", Font.PLAIN, 20));
		textFieldBig.setColumns(10);
		textFieldBig.setBounds(282, 133, 146, 31);
		contentPane.add(textFieldBig);
		setResizable(false);
	}
	
	public void setNameList(String[] names){
		cmbBoxName.setModel(new DefaultComboBoxModel<String>(names));
	}
	
	public void paintView(){
		String name = (String)cmbBoxName.getSelectedItem();
		if(name == null){
			return;
		}
		if(isConfirmed == false && user != null){
			System.out.println("aklfjdkljf");
			user.addRestPoints(singleValue);
			user.addRestPoints(doubleValue);
			user.addRestPoints(smallValue);
			user.addRestPoints(bigValue);
			user.addRestPoints(bigSingleValue);
			user.addRestPoints(bigDoubleValue);
			user.addRestPoints(smallSingleValue);
			user.addRestPoints(smallDoubleValue);
			for(int i = 0; i < User.NUM; i++){
				user.addRestPoints(eachPintArray[i]);
			}
			user.addRestPoints(eachSinglePointValue);
		}
		for(int i = 0; i < User.NUM; i++){
			eachPintArray[i] = 0;
		}
		singleValue = 0;
		doubleValue = 0;
		smallValue = 0;
		bigValue = 0;
		bigSingleValue = 0;
		bigDoubleValue = 0;
		smallSingleValue = 0;
		smallDoubleValue = 0;
		eachSinglePoint = 0;
		eachSinglePointValue = 0;
		textFieldSingle.setText(null);
		textFieldDouble.setText(null);
		textFieldSmall.setText(null);
		textFieldBig.setText(null);
		textFieldBigSingle.setText(null);
		textFieldBigDouble.setText(null);
		textFieldSmallSingle.setText(null);
		textFieldSmallDouble.setText(null);
		textFieldEachPoint.setText(null);
		cmbBoxEachPoint.setSelectedIndex(0);
		user = Launcher.getUserByName(name);
		int[] tempArray = user.getEachPoints();
		for(int i = 0; i < User.NUM; i++){
			eachPintArray[i] = tempArray[i];
		}
		labelRestPoints.setText(user.getRestPoints() + "");
		if(user.getSinglePoints() > 0){
			textFieldSingle.setText(user.getSinglePoints() + "");
			singleValue = user.getSinglePoints();
		}
		if(user.getDoublePoints() > 0){
			textFieldDouble.setText(user.getDoublePoints() + "");
			doubleValue = user.getDoublePoints();
		}
		if(user.getBigPoints() > 0){
			textFieldBig.setText(user.getBigPoints() + "");
			bigValue = user.getBigPoints();
		}
		if(user.getSmallPoints() > 0){
			textFieldSmall.setText(user.getSmallPoints() + "");
			smallValue = user.getSmallPoints();
		}
		if(user.getBigSinglePoints() > 0){
			textFieldBigSingle.setText(user.getBigSinglePoints() + "");
			bigSingleValue = user.getBigSinglePoints();
		}
		if(user.getBigDoublePoints() > 0){
			textFieldBigDouble.setText(user.getBigDoublePoints() + "");
			bigDoubleValue = user.getBigDoublePoints();
		}
		if(user.getSmallSinglePoints() > 0){
			textFieldSmallSingle.setText(user.getSmallSinglePoints() + "");
			smallSingleValue = user.getSmallSinglePoints();
		}
		if(user.getSmallDoublePoints() > 0){
			textFieldSmallDouble.setText(user.getSmallDoublePoints() + "");
			smallDoubleValue = user.getSmallDoublePoints();
		}
		if(user.getEachPoints()[0] == 0){
			textFieldEachPoint.setText(null);
		}else{
			textFieldEachPoint.setText(user.getEachPoints()[0] + "");
		}
		eachSinglePointValue = user.getEachPoints()[0];
		eachSinglePoint = 0;
	}
	
	private void stopCharacter(KeyEvent e){
		isConfirmed = false;
		char keyChar = e.getKeyChar();
		if(!((keyChar >= '0' && keyChar <= '9'))){
			e.consume();
		}
	}
	
	protected void processWindowEvent(WindowEvent e){
		if(e.getID() == WindowEvent.WINDOW_CLOSING){
		}else{
			super.processWindowEvent(e);
		}
	}
	public void confirmBetting(){
		if(user == null){
			return;
		}
		user.setSinglePoints(singleValue);
		user.setDoublePoints(doubleValue);
		user.setSmallPoints(smallValue);
		user.setBigPoints(bigValue);
		user.setBigSinglePoints(bigSingleValue);
		user.setBigDoublePoints(bigDoubleValue);
		user.setSmallSinglePoints(smallSingleValue);
		user.setSmallDoublePoints(smallDoubleValue);
		eachPintArray[eachSinglePoint] = eachSinglePointValue;
		user.setEachPoints(eachPintArray);
		isConfirmed = true;
	}
}
